<template>
  <div class="system-log-container">
    <div class="spaceForm">
      <el-form :inline="true" :model="queryForm">
        <el-form-item>
          <el-select
            v-model="queryForm.group_id"
            :clearable="true"
            placeholder="分组"
            @change="queryData"
          >
            <el-option
              v-for="item in group"
              :key="item.id"
              :label="item.group_name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="queryForm.room_type"
            :clearable="true"
            placeholder="类型"
            @change="queryData"
          >
            <el-option label="整套" value="1" />
            <el-option label="合租" value="2" />
            <el-option label="整栋" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="queryForm.rent_status"
            :clearable="true"
            placeholder="状态"
            @change="queryData"
          >
            <el-option
              v-for="item in dictList"
              :key="item.item_code"
              :label="item.item_name"
              :value="item.item_code"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model.trim="queryForm.keyword"
            clearable
            placeholder="请输入关键词"
            @keyup.enter.native="queryData"
            @clear="queryData"
          />
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="queryData">
            查询
          </el-button>
        </el-form-item>
      </el-form>
      <el-form>
        <el-button @click="getExcel">导出表格</el-button>
      </el-form>
    </div>
    <el-table v-loading="listLoading" :data="list">
      <el-table-column label="入住人" min-width="120" prop="customer_name" />
      <el-table-column
        label="联系方式"
        min-width="160"
        prop="customer_mobile"
      />
      <el-table-column
        label="地址"
        min-width="200"
        prop="room_address"
        show-overflow-tooltip
      />
      <el-table-column label="入住周期" min-width="200">
        <template #default="{ row }">
          <div>{{ row.start_date }}至{{ row.end_date }}</div>
        </template>
      </el-table-column>
      <el-table-column label="状态" min-width="100">
        <template #default="{ row }">
          <div v-for="item in dictList" :key="item.id">
            <div v-if="row.rent_status == item.item_code">
              {{ item.item_name }}
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="分组" min-width="100" prop="group_name" />
      <el-table-column align="center" label="操作" width="85">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">详情</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-image
          class="vab-data-empty"
          :src="require('@/assets/empty_images/data_empty.png')"
        />
      </template>
    </el-table>
    <rent-detail ref="rentDetail" @fetch-data="queryData" />
    <el-pagination
      background
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.perpage"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script>
  import { getRentList, deleteRent } from '@/api/rent'
  import { getgroup } from '@/api/group'
  import { getDictByCode } from '@/utils/dict'
  import { parseTime } from '@/utils/index'
  import rentDetail from '@/views/components/rentDetail'
  export default {
    name: 'CustomerIndex',
    components: { rentDetail },
    data() {
      return {
        list: [],
        group: [],
        dictList: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {
          group_id: '',
          room_type: '',
          rent_status: '',
          keyword: '',
          page: 1,
          perpage: 20,
        },
      }
    },
    created() {
      this.getGroup()
      this.getDict()
      this.fetchData()
    },
    methods: {
      handleSizeChange(val) {
        this.queryForm.perpage = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.page = 1
        this.fetchData()
      },
      handleEdit(row) {
        this.$refs['rentDetail'].showEdit(row)
      },
      handleDelete(row) {
        this.$baseConfirm(
          '删除后，入住记录、合同信息、账单信息将被删除，不可恢复！',
          null,
          async () => {
            const { data } = await deleteRent({ rent_sn: row.rent_sn })
            if (data.is_success) {
              this.$baseMessage(
                '删除成功',
                'success',
                'vab-hey-message-success'
              )
              this.fetchData()
            }
          }
        )
      },
      async fetchData() {
        this.listLoading = true
        var queryData = Object.assign({}, this.queryForm)
        if (queryData.group_id == '') {
          delete queryData.group_id
        }
        if (queryData.room_type == '') {
          delete queryData.room_type
        }
        if (queryData.rent_status == '') {
          delete queryData.rent_status
        }
        if (queryData.keyword == '') {
          delete queryData.keyword
        }
        const { data } = await getRentList(queryData)
        this.list = data.items
        this.total = data.total
        this.listLoading = false
      },
      async getGroup() {
        const { data } = await getgroup({})
        this.group = data
      },
      async getDict() {
        this.dictList = await getDictByCode('rent_status')
      },
      getExcel() {
        import('@/utils/excel').then((excel) => {
          const header = [
            'ID',
            '入住人',
            '联系方式',
            '地址',
            '入住周期',
            '状态',
            '分组',
          ]
          const filterVal = [
            'id',
            'customer_name',
            'customer_mobile',
            'room_address',
            'date',
            'rent_status_name',
            'group_name',
          ]
          const list = Object.assign([], this.list)
          for (let i = 0; i < list.length; i++) {
            list[i].date = list[i].start_date + '至' + list[i].end_date
            for (let j = 0; j < this.dictList.length; j++) {
              if (list[i].rent_status == this.dictList[j].item_code) {
                list[i].rent_status_name = this.dictList[j].item_name
              }
            }
          }
          const data = this.formatJson(filterVal, list)
          const filename = 'rent' + new Date().valueOf()
          excel.export_json_to_excel({
            header,
            filename,
            data,
          })
        })
      },
      formatJson(filterVal, jsonData) {
        return jsonData.map((v) =>
          filterVal.map((j) => {
            if (j === 'timestamp') {
              return parseTime(v[j])
            } else {
              return v[j]
            }
          })
        )
      },
    },
  }
</script>
<style lang="scss" scoped>
  .spaceForm .el-select {
    width: 130px;
  }
</style>
